package com.lagou.demo.controller;

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description 类功能描述
 * @Author 郭汝诚
 * @Date 2020/6/21 18:05
 * @Version 1.0
 **/
@Controller
public class ChangeDateSourceController {

    @Value("${dataSourcePath}")
    private String dataSourcePath;

    @Autowired
    private CuratorFramework client;

    @RequestMapping("/change")
    @ResponseBody
    public String index(String db) throws Exception {
        //String context = "url=jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC&characterEncoding=utf-8\n" +
        String context = "url=jdbc:mysql://localhost:3306/" + db + "?serverTimezone=UTC&characterEncoding=utf-8\n" +
                "username=root\n" +
                "password=root\n" +
                "driver-class-name=com.mysql.cj.jdbc.Driver";

        //client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(dataSourcePath, context.getBytes());

        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath(dataSourcePath);

        client.setData().withVersion(stat.getVersion()).forPath(dataSourcePath,context.getBytes());
        return "success";
    }


}
